package org.punkliner;

import java.util.Timer;
import java.util.TimerTask;

import org.punkliner.in.SmbListener;
import org.punkliner.out.LogWriter;
import org.punkliner.out.TableWriter;

public class Main {

	public static SmbListener   listener;
	public static Timer         timer;

	private static final String VERSION = "0.1";

	public static void main(String[] argv) {
		if (argv.length != 0 && argv[0].equals("--help")) {
			System.out.println("PUNKLiner (http://code.google.com/p/punkliner), version " + VERSION);
			System.out.println("Passive discovery of online hosts in local area networks");
			System.out.println("Usage: java -jar punkliner-{version}.jar [number]");
			System.out.println("\tnumber\tnumber of seconds to run, will run forever if omitted");
			System.exit(0);
		} else if (argv.length != 0 && argv[0].equals("--version")) {
			System.out.println("PUNKLiner (http://code.google.com/p/punkliner), version " + VERSION);
			System.exit(0);
		}

		listener = new SmbListener();
		listener.addWriter(new LogWriter());
		listener.addWriter(new TableWriter());
		listener.start();

		System.out.println("PUNKLiner successfully started.");

		if (argv.length > 0) {
			int seconds = Integer.parseInt(argv[0]);
			if (seconds != 0) {
				timer = new Timer();
				timer.schedule(new RunWatchDog(), 1000 * seconds);
				System.out.println("PUNKLiner scheduled to stop in " + seconds + " seconds.");
			}
		} else {
			System.out
			        .println("PUNKLiner will now run forever (if you want it be stopped either interrupt the process or launch with arguments).");
		}
	}

}

class RunWatchDog extends TimerTask {

	@Override
	public void run() {
		Main.listener.interrupt();
		Main.timer.cancel();
		System.out.println("PUNKLiner stopped.");
	}

}
